﻿Imports lbGlobalcom
Public Class fTercero
    Dim daTerce As lbGlobalcom.dsTerceroTableAdapters.TER_TERCEROTableAdapter
    Dim daMuni As lbGlobalcom.dsGlobalTableAdapters.GLO_MUNICIPIOSTableAdapter
    Dim daDatTab As lbGlobalcom.dsGlobalTableAdapters.GLO_DATOSTABLASTableAdapter
    Private lMuestraMsjSalva As Boolean = True
    Public Event SalvaTerecero()
    Public Event EliminaTercero()
    Public Event AddTercero()
    Public Event BuscaTercero(ByVal idTercero As String)
    Public Event ExporToExcel()

    Public Property MustraMsjSalva() As Boolean
        Get
            Return lMuestraMsjSalva
        End Get
        Set(ByVal value As Boolean)
            lMuestraMsjSalva = value
        End Set
    End Property
    Private Sub btnBuscaMunicipio_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBuscaMunicipio.Click
        Dim fListaM As New fListaMunicipios
        If fListaM.ShowDialog = Windows.Forms.DialogResult.OK Then
            IDMUNICIPIOTextBox.Text = fListaM.IdMunicipio
        End If
    End Sub

    Private Sub IDMUNICIPIOTextBox_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles IDMUNICIPIOTextBox.TextChanged
        Try
            If IDMUNICIPIOTextBox.Text.Trim.Length > 0 Then
                Me.lbMunicipio.Text = daMuni.getNombreMunicipio(IDMUNICIPIOTextBox.Text)
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message)
            lbGlobalcom.cError.SalvaError(ex, "Samuel")
        End Try

    End Sub

    Private Sub fTercero_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Try
            daTerce = New lbGlobalcom.dsTerceroTableAdapters.TER_TERCEROTableAdapter
            daMuni = New lbGlobalcom.dsGlobalTableAdapters.GLO_MUNICIPIOSTableAdapter
            daDatTab = New lbGlobalcom.dsGlobalTableAdapters.GLO_DATOSTABLASTableAdapter
            daDatTab.FillByIdGrupo(DsGlobalEstado.GLO_DATOSTABLAS, "es")
            daDatTab.FillByIdGrupo(DsGlobal.GLO_DATOSTABLAS, "di")
            daDatTab.FillByIdGrupo(DsGlobalRegimen.GLO_DATOSTABLAS, "tr")
            lbGlobalcom.cGeneral.ActivaControles(Me.Controls, False)
        Catch ex As Exception
            MessageBox.Show(ex.Message)
            lbGlobalcom.cError.SalvaError(ex, "Samuel")
        End Try
    End Sub

    Private Sub btnBuscaTercero_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBuscaTercero.Click
        Try
            Dim fBusTer As New fBuscaTercero
            If fBusTer.ShowDialog = Windows.Forms.DialogResult.OK Then
                If DsTercero.TER_TERCERO.GetChanges IsNot Nothing Or DsTercero.TER_PROVEEDORES.GetChanges IsNot Nothing Or DsTercero.TER_CLIENTES.GetChanges IsNot Nothing Or DsTercero.TER_VENDEDOR.GetChanges IsNot Nothing Then
                    If MessageBox.Show("Hay cambios pendentes por salvar,Desea salvarlos", "Hay Cambios", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.Yes Then
                        daTerce.Update(DsTercero.TER_TERCERO)
                    End If
                End If
                DsTercero.TER_PROVEEDORES.Rows.Clear()
                DsTercero.TER_CLIENTES.Rows.Clear()
                DsTercero.TER_VENDEDOR.Rows.Clear()
                daTerce.FillByIdTercero(DsTercero.TER_TERCERO, fBusTer.midTercero)
                lbGlobalcom.cGeneral.ActivaControles(Me.Controls, True)
                RaiseEvent BuscaTercero(fBusTer.midTercero)
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message)
            lbGlobalcom.cError.SalvaError(ex, "Samuel")
        End Try

    End Sub

    Private Sub TER_TERCEROBindingNavigatorSaveItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TER_TERCEROBindingNavigatorSaveItem.Click
        Try
            Validate()
            TER_TERCEROBindingSource.EndEdit()
            daTerce.Update(DsTercero.TER_TERCERO)
            RaiseEvent SalvaTerecero()
            If lMuestraMsjSalva Then _
                MessageBox.Show("Se salvo tercero con exito", "salva Terceros", MessageBoxButtons.OK, MessageBoxIcon.Information)
        Catch ex As Exception
            MessageBox.Show(ex.Message)
            lbGlobalcom.cError.SalvaError(ex, "Samuel")
        End Try
    End Sub

    Private Sub BindingNavigatorDeleteItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BindingNavigatorDeleteItem.Click
        Try
            If MessageBox.Show("Esta seguro que desea eliminar este tercero", "Elimina Tercero", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.Yes Then
                RaiseEvent EliminaTercero()
                TER_TERCEROBindingSource.Remove(TER_TERCEROBindingSource.Current)
                daTerce.Update(DsTercero.TER_TERCERO)
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message)
            lbGlobalcom.cError.SalvaError(ex, "Samuel")
        End Try
    End Sub


    Private Sub BindingNavigatorAddNewItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BindingNavigatorAddNewItem.Click
        Try
            TER_TERCEROBindingSource.EndEdit()
            If DsTercero.GetChanges IsNot Nothing Then
                If MessageBox.Show("Hay cambios pendientes por salvar, desea salvarlos", "Hay Cambios", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.Yes Then
                    lMuestraMsjSalva = False
                    TER_TERCEROBindingNavigatorSaveItem_Click(Nothing, Nothing)
                End If
            End If
            DsTercero.TER_CLIENTES.Rows.Clear()
            DsTercero.TER_PROVEEDORES.Rows.Clear()
            DsTercero.TER_VENDEDOR.Rows.Clear()
            DsTercero.TER_TERCERO.Rows.Clear()
            TER_TERCEROBindingSource.AddNew()
            TER_TERCEROBindingSource.EndEdit()
            RaiseEvent AddTercero()
            lbGlobalcom.cGeneral.ActivaControles(Me.Controls, True)
            IDTERCEROTextBox.Focus()
        Catch ex As Exception
            MessageBox.Show(ex.Message)
            lbGlobalcom.cError.SalvaError(ex, "Samuel")
        End Try

    End Sub

    Private Sub DVTextBox_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles DVTextBox.Validating
        Try
            If DOCIDENTListBox.SelectedValue = 1 AndAlso IDTERCEROTextBox.Text.Trim.Length > 0 And IDTERCEROTextBox.Text.Trim.Length > 0 AndAlso IsNumeric(IDTERCEROTextBox.Text) Then
                If Not cConta.ValidarDigitoVerificacion(IDTERCEROTextBox.Text, DVTextBox.Text) Then
                    MessageBox.Show("Digito de verificación no corresponde con este Nit, favor verificar e intentar de nuevo " & vbNewLine & "El digito de verificación para este nit es: " & cConta.ldv.ToString, "Nit o Digito invalido", MessageBoxButtons.OK, MessageBoxIcon.Stop)
                    e.Cancel = True
                End If
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
        
    End Sub

    Private Sub btnValidaTer_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnValidaTer.Click
        Try
            Dim ter As New cTercero(IDTERCEROTextBox.Text, "Ter")
            If ter.Existe Then
                MessageBox.Show("Ya existe tercero con este nit, favor verificar", "Tercero Existe", MessageBoxButtons.OK, MessageBoxIcon.Stop)
                IDTERCEROTextBox.Focus()
            Else
                DVTextBox.Focus()
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub

    Private Sub btnExportaAExcel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExportaAExcel.Click
        Try
            RaiseEvent ExporToExcel()
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub
End Class